package com.chaosj.skill.future

import java.util.concurrent.CompletableFuture

/**
 * @Description:
 * @author: caoyangjie
 * @date: 2019年11月2019/11/21日 09:32
 * @Copyright: © 赢时胜
 */
class CompletableFutureStudy {
    static void main(String[] args){
//        System.out.println(CompletableFuture.supplyAsync({
//            "hello"
//        }).thenApplyAsync({x->
//            System.out.println("haha")
//            x+"world"
//        }).thenApplyAsync({x->
//            System.out.println("ahah")
//            x
//        }).get())

        System.out.println("开始异步执行别的工作")

        doSomeThingWhenComplete()

        while (true){
            System.out.println("主线程还可以做数学哦")
            Thread.sleep(1000L)
        }
    }

    private static CompletableFuture<String> doSomeThingWhenComplete() {
        CompletableFuture.supplyAsync({
            System.out.println("1")
            Thread.sleep(2000L)
            System.out.println("2")
            "hello"
        }).thenApplyAsync({ value ->
            System.out.println("3")
            Thread.sleep(5000L)
            System.out.println("4")
            value + "world!"
        }).whenComplete({ x, y ->
            System.out.println("5")
            System.out.println(x)
            System.out.println(y)
            System.out.println("6")
            x + y
        })
    }
}
